package  com.zhb.cloud.utils;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * @ClassName: DateTimeUtil
 * @Description:日期处理工具类
 * @author: zhb1316783262@163.com
 * @date: 2019年2月21日下午2:10:42
 */
public class DateUtil {

	// 年月日
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	// 年月日 时分秒
	public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

	// str->Date
	public static Date strToDate(String dateTimeStr) {
		DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATE_FORMAT);
		DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
		return dateTime.toDate();
	}

	// Date->str
	public static String dateToStr(Date date, String formatStr) {
		if (date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(formatStr);
	}

	// str->Date
	public static Date strToDateTime(String dateTimeStr) {
		DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
		DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
		return dateTime.toDate();
	}

	// Date->str
	public static String dateToStr(Date date) {
		if (date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(STANDARD_FORMAT);
	}

}
